package com.forensix.interface_test.service;

import com.forensix.interface_test.utils.*;
import io.qameta.allure.*;
import io.qameta.allure.restassured.AllureRestAssured;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import static io.restassured.RestAssured.given;

public class HttpBinApiImpl implements HttpBinApi{

    String httpBinUrl = ReadPropertiesUtil.ReadProperties("httpbin");

    String get = "/get";

    String post = "/post";

    String put = "/put";

    String delete = "/delete";

    HashMap<String, Object> hashMap = new HashMap<>();

    @BeforeTest
    public void setUp(){

        /*
        * 在每个测试类之前执行一次
        * */
        RestAssured.filters(new AllureRestAssured());

    }


    @Epic("测试")
    @Story("测试")
    @Severity(SeverityLevel.BLOCKER)
    @Step("测试")
    @Description("测试")
    @Test(priority = 1, description = "get请求")
    @Override
    public void get() {


//        Allure.step("get请求", ()->{  // Allure.step(String name, ()->{}) 作为一个步骤记录到allure报告中
//
//            String response = given()
//                    .when().log().all()
//                    .get(httpBinUrl + get)
//                    .then()
//                    .log().all().extract().asString();
//
//            Allure.attachment("log", response);  // 将 响应打印到 Allure 报告中
//
//        });

//
//        RestAssured.filters(new AllureRestAssured());
        given()
                .when().log().all()
                .get(httpBinUrl + get)
                .then()
                .log().all().extract().asString();


    }

    @Test(priority = 2)
    @Override
    public void getParams() {

        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");
//        RestAssured.filters(new AllureRestAssured());

//        PrintStreamLogUtil.printStreamLog(); // stream 流 记录请求与响应的日志
        given().contentType("application/json; charset=UTF-8").when().log().all()
                .params(hashMap)
                .get(httpBinUrl + get)
                .then()
                .log().all()
                .statusCode(200);

//        AllureAttachmentUtil.addHttpLogToAllure(); // 将日志 记录到allure报告中
    }

    @Test(priority = 7)
    @Override
    public void put() {

//        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .put(httpBinUrl + put)
                .then().log().all();
//        AllureAttachmentUtil.addHttpLogToAllure();

    }

    @Test(priority = 8)
    @Override
    public void putFormData() {
        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");

        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .formParams(hashMap)
                .multiPart("test", new File(GetBaseDirUtil.getBaseDir() + "\\data\\image\\1.jpg"))
                .multiPart("test1", "1234567")
                .put(httpBinUrl + put)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }


    @Test(priority = 9)
    @Override
    public void putFormUrlencodedData() {
        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");
        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .formParams(hashMap)
                .put(httpBinUrl + put)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }

    @Test(priority = 10)
    @Override
    public void putRawJsonData() {
        PrintStreamLogUtil.printStreamLog();
        given().contentType(ContentType.JSON).
                body(new File(GetBaseDirUtil.getBaseDir() + "/data/data.json"))
                .when().log().all()
                .put(httpBinUrl+ put)
                .then()
                .log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }

    @Test(priority = 3)
    @Override
    public void post() {

        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .post(httpBinUrl + post)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();

    }

    @Test(priority = 4)
    @Override
    public void postFormData() {

        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");

        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .formParams(hashMap)
                .multiPart("test", new File(GetBaseDirUtil.getBaseDir() + "\\data\\image\\1.jpg"))
                .multiPart("test1", "1234567")
                .post(httpBinUrl + post)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }

    @Test(priority = 5)
    @Override
    public void postFormUrlencodedData() {
        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");
        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .formParams(hashMap)
                .post(httpBinUrl + post)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();

    }

    @Test(priority = 6)
    @Override
    public void postRawJsonData() {
//
//        String json = "{\n" +
//                "  \"name\": \"John Doe\",\n" +
//                "  \"age\": 32,\n" +
//                "  \"email\": \"john.doe@example.com\",\n" +
//                "  \"address\": {\n" +
//                "    \"street\": \"123 Main St\",\n" +
//                "    \"city\": \"Anytown\",\n" +
//                "    \"state\": \"CA\",\n" +
//                "    \"zip\": \"12345\"\n" +
//                "  },\n" +
//                "  \"hobbies\": [\"reading\", \"hiking\", \"traveling\"]\n" +
//                "}";

        PrintStreamLogUtil.printStreamLog();
        given().contentType(ContentType.JSON).
                body(new File(GetBaseDirUtil.getBaseDir() + "/data/data.json"))
                .when().log().all()
                .post(httpBinUrl+ post)
                .then()
                .log().all();
        AllureAttachmentUtil.addHttpLogToAllure();


    }

    @Test(priority = 11)
    @Override
    public void delete() {

        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .delete(httpBinUrl + delete)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();

    }


    @Test(priority = 12)
    @Override
    public void deleteFormData() {

        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");

        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .formParams(hashMap)
                .multiPart("test", new File(GetBaseDirUtil.getBaseDir() + "\\data\\image\\1.jpg"))
                .multiPart("test1", "1234567")
                .delete(httpBinUrl + delete)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }

    @Test(priority = 13)
    @Override
    public void deleteFormUrlencodedData() {
        hashMap.put("test", "123456");
        hashMap.put("test1", "123456");
        PrintStreamLogUtil.printStreamLog();
        given().when().log().all()
                .formParams(hashMap)
                .delete(httpBinUrl + delete)
                .then().log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }

    @Test(priority = 14)
    @Override
    public void deleteRawJsonData() {
        PrintStreamLogUtil.printStreamLog();
        given().contentType(ContentType.JSON).
                body(new File(GetBaseDirUtil.getBaseDir() + "/data/data.json"))
                .when().log().all()
                .delete(httpBinUrl+ delete)
                .then()
                .log().all();
        AllureAttachmentUtil.addHttpLogToAllure();
    }


    /**
     * 保存文件到本地
     */
    @Test
    public void loadLocalFile(){

//        Response response = given().log().all()
//                .cookie("Cookies", "123456")
//                .when()
//                .get("http://localhost:8080") // 下载链接地址
//                .then()
//                .log().all().extract().response();
//
//        byte[] responseByteArray = response.asByteArray();  // 二进制接受响应内容
//        try {
//            // 下载是什么格式 书写什么格式
//            // /load_file/1.jpg(png zip excel mp3 mp4 .......)
//            FileOutputStream fileOutputStream = new FileOutputStream(GetBaseDirUtil.getBaseDir() + "/load_file/1.jpg");
//            fileOutputStream.write(responseByteArray);
//            fileOutputStream.close();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
    }


}
